/**
 * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
 *
 * 
 * @param order int整型二维数组 订单信息
 * @return int整型
 */
function delivery (order) {
  // write code here
  order.sort((a, b) => {
    if (a[0] === b[0]) {
      return a[1] - b[1]
    } else {
      return a[0] - b[0]
    }
  })
  let step = order[0][0] + order[0][1]
  let martix = {}
  for (let i = 0; i < order.length; i++) {
    let [a, b] = order[i]
    if (!martix[a]) {
      martix[a] = []
    }
    martix[a].push(b)
  }
  console.log(martix);
  let keys = Object.keys(martix)
  keys.sort((a, b) => a * 1 - b * 1)
  console.log(keys);
  let forword = 0
  for (let i = 0; i < keys.length; i++) {
    if (i !== 0) {

    } else {
      step += martix[keys[i]][martix[keys[i]].length - 1] - martix[keys[i]][0]

      // if(i % 2 === 0){
      //   forword = martix[keys[i]][martix[keys[i]].length-1]
      // }else{
      //   forword = martix[keys[i]][0]
      // }

    }
  }
  // keys.forEach((item)=>{
  //   step += martix[item][martix[item].length-1] - martix[item][0]
  // })
}
// let order = [[1, 1], [1, 2], [2, 2], [1, 3], [3, 3]]
// console.log(delivery(order));
module.exports = {
  delivery: delivery
};